package com.waya.web.browser.common.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.waya.web.browser.common.show.FL;
import com.waye.wanba.utils.APPLog;

import java.io.File;

/**
 * Created by Aria.Lao on 2017/3/2.
 */

public class ApkUtil {

    private static final String TAG = "ApkUtil";

    private static final String REGEX = ",";

    /**
     * 卸载APk
     *
     * @param packageName 包名
     */
    public static void uninstall(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            FL.e(TAG, "packageName is not null!");
            return;
        }
        AndroidUtils.uninstall(context, packageName);
    }

    /**
     * 安装APK
     *
     * @param apkPath apk路径
     */
    public synchronized static void installApk(Context context, String apkPath) {
        File apk = new File(apkPath);
        if (!apk.exists()) {
            FL.e(TAG, "路径【" + apkPath + "】不存在！！");
            return;
        }
        context.startActivity(AndroidUtils.getInstallIntent(new File(apkPath)));
    }


    /**
     * 获取版本号
     */
    public static int getAppVersion(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            FL.e(TAG, "packageName is not null!");
            return -1;
        }
        return AndroidUtils.getAppVersionCode(context, packageName);
    }

    public static boolean isInstall(Context context, String packageName) {
        if (TextUtils.isEmpty(packageName)) {
            FL.e(TAG, "packageName is not null!");
            return false;
        }
        return AndroidUtils.isInstall(context, packageName);
    }

    /**
     * 启动第三方app
     *
     * @param context      context
     * @param packageName  包名
     * @param activityName activityName 第三方应用activity全称。
     * @param actions      第三方应用intent-filter中的action，多个action以’,‘分隔。
     * @param categorys    第三方应用intent-filter中的categroy，多个categroy以’,‘分隔。
     * @param datas        第三方应用intent-filter中的data，多个data以’,‘分隔。
     * @param params       第三方应用启动时需要的参数，第三方应用应该以params为key从Intent中获取params的值
     */
    public static void startApp(Context context, String packageName, String activityName,
                                String actions, String categorys, String datas, String params) {
        try {
            Intent intent = new Intent();
            ComponentName componentName = new ComponentName(packageName, activityName);
            intent.setComponent(componentName);
            if (TextUtils.isEmpty(actions)) {
                intent.setAction("android.intent.action.MAIN");
            }
            AndroidUtils.setIntentData(intent, datas, REGEX, "data");
            AndroidUtils.setIntentData(intent, actions, REGEX, "action");
            AndroidUtils.setIntentData(intent, categorys, REGEX, "category");
            if (!TextUtils.isEmpty(params)) intent.putExtra("params", params);
            context.startActivity(intent);
        } catch (Exception e) {
            FL.e(TAG, FL.getExceptionString(e));
        }
    }

    /**
     * 下载并安装apk
     *
     * @param context
     * @param downloadUrl 下载地址
     */
    public static void downLoadAndInstallApk(final Context context, final String downloadUrl) {
        if (TextUtils.isEmpty(downloadUrl)) {
            FL.e(TAG, "url is not null!");
            return;
        }
        String path = AndroidUtils.getDownloadDir(downloadUrl);
        final File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        DownloadUtil.getInstance().download(downloadUrl, file,
                new DownloadUtil.AbsResponse() {
                    @Override
                    public void onResponse(String data) {
                        super.onResponse(data);
                        APPLog.printInfo("apk存储路径===》" + data);
                        ApkUtil.installApk(context, file.getAbsolutePath());
                    }

                    @Override
                    public void onError(String error) {
                        super.onError(error);
                        FL.e(TAG, error);
                    }
                });
    }

}
